/// <reference path="../jquery.min.js" />
/// <reference path="rsseasy.v2.js" />

//通用接口
var RssApi = {}
RssApi.Table = {}
RssApi.Table.List = function (name) {
    return new Ajax("table/list/" + name);
}
RssApi.Table.Detail = function (name) {
    return new Ajax("table/detail/" + name);
}
//条件中myid、heads头部usertoken必传
RssApi.Table.Append = function (name) {
    return new Ajax("table/append/" + name);
}
//条件中myid、heads头部usertoken必传
RssApi.Table.Delete = function (name) {
    return new Ajax("table/delete/" + name);
}
//条件中myid、heads头部usertoken必传
RssApi.Table.Update = function (name) {
    return new Ajax("table/update/" + name);
}
RssApi.Table.Edit = function (name) {
    return new Ajax("table/edit/" + name);
}
RssApi.Table.Query = function (name) {
    return new Ajax("table/query/" + name);
}

//用户
RssApi.User = {}
RssApi.User.Update = function () {
    return new Ajax("user/update");
}
RssApi.User.AppReg = function () {
    return new Ajax("user/bind/openid");
}
RssApi.User.Device = function () {
    return new Ajax("user/device");
}
//用户地址
RssApi.UserAddr = {}
RssApi.UserAddr.Edit = function () {
    return new Ajax("user/addr/edit");
}
RssApi.UserAddr.List = function () {
    return new Ajax("user/addr/list");
}
RssApi.UserAddr.Delete = function () {
    return new Ajax("user/addr/delete");
}
//提现
RssApi.Withdraw = {}
RssApi.Withdraw.Append = function () {
    return new Ajax("withdraw/append");
}

//点赞 参数：myid,relationid
RssApi.Table.Praise = function (name) {
    return new Ajax("table/praise/" + name);
}
RssApi.Table.onPraise = function () { }
//评论 参数：myid,relationid,title,matter
RssApi.Table.Comment = function (name) {
    return new Ajax("table/comment/" + name);
}
RssApi.Table.onComment = function () { }
//报名 参数：myid,relationid
RssApi.Table.Apply = function (name) {
    return new Ajax("table/apply/" + name);
}
//收藏 参数：myid,relationid
RssApi.Table.Collect = function (name) {
    return new Ajax("table/collect/" + name);
}
RssApi.Table.onCollect = function () { }
//关注
RssApi.Table.Attention = function (name) {
    return new Ajax("table/attention/" + name);
}
RssApi.Table.onAttention = function () { }


//常用功能接口

//关注
$("body").on("click", "[ry-attention]", function (ev) {
    ev.preventDefault();
    ev.stopPropagation();
    var t = $(this), module = t.attr("ry-attention"), dataid = t.attr("relationid");
    if (dataid && RssUser.Data.myid) {
        RssApi.Table.Attention(module).keyvalue("relationid", dataid).keymyid().getJson(function (json) {
            t.attr("isattention", json["result"]);
            RssApi.Table.onAttention(json);
        });
    }
})
//点赞
$("body").on("click", "[ry-praise]", function (ev) {
    ev.preventDefault();
    ev.stopPropagation();
    var t = $(this), module = t.attr("ry-praise"), dataid = t.attr("relationid") || t.attr("rssid") || "";
    if (dataid && RssUser.Data.myid) {
        RssApi.Table.Praise(module).keyvalue("relationid", dataid).keymyid().getJson(function (json) {
            t.attr("ispraise", json["result"]).html(parseInt(t.html()) + (json["result"] ? +1 : -1));
            RssApi.Table.onPraise(json);
        });
    }
})
//报名
$("body").on("click", "[ry-apply]", function (ev) {
    ev.preventDefault();
    ev.stopPropagation();
    var t = $(this), module = t.attr("ry-apply"), dataid = t.attr("relationid") || t.attr("rssid") || "";
    if (dataid && RssUser.Data.myid) {
        RssApi.Table.Praise(module).keyvalue("relationid", dataid).keymyid().getJson(function (json) {
            t.attr("isapply", json["result"]).html(parseInt(t.html()) + (json["result"] ? +1 : -1));
        });
    }
})
//收藏
$("body").on("click", "[ry-collect]", function (ev) {
    ev.preventDefault();
    ev.stopPropagation();
    var t = $(this), module = t.attr("ry-collect"), dataid = t.attr("relationid");
    if (dataid && RssUser.Data.myid) {
        RssApi.Table.Collect(module).keyvalue("relationid", dataid).keymyid().getJson(function (json) {
            t.attr("iscollect", json["result"]);
            RssApi.Table.onCollect(json);
        });
    }
})